package com.starwish.starwishblog.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.mapper.CommentMapper;
import com.starwish.starwishblog.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentService {
    @Autowired
    CommentMapper commentMapper;

    /*------查询所有子评论--------------------------------------*/
    public PageInfo<CommentVo> findChildCommentByPage(Integer now, Integer size, String content,String username,String title,Integer rootId) {
        PageHelper.startPage(now,size);
        List<CommentVo> lists = commentMapper.selectChildCommentBycCons(content,username,title,rootId);
        return new PageInfo<>(lists);
    }

    /*------查询所有一级根评论--------------------------------------*/
    public PageInfo<CommentVo> findRootCommentByPage(Integer now, Integer size, String content,String username,String title) {
        PageHelper.startPage(now,size);
        List<CommentVo> lists = commentMapper.selectRootCommentByCons(content,username,title);
        return new PageInfo<>(lists);
    }

    public int addComment(CommentVo commentVo) {
        return commentMapper.insert(commentVo);
    }

    public int removeComment(Integer id,Integer status) {
        return commentMapper.deleteById(id,status);
    }

    public int modifyComment(CommentVo commentVo) {
        return commentMapper.updateByPrimaryKey(commentVo);
    }



    public PageInfo<CommentVo> findCommentsByArticleId(Integer now,Integer size,Integer aid){
        //查找该id下文章 的level等于1评论列表
        PageHelper.startPage(now,size);
        List<CommentVo> commentVos = commentMapper.selectByArticleIdIsParent(aid);
        if(commentVos != null){
            for (CommentVo commentVo : commentVos) {
                //如果level等于1，要去查询有没有子评论
                Integer id = commentVo.getId();
                List<CommentVo> children = commentMapper.selectByParentId(id);
                commentVo.setChildren(children);
            }
        }
        return new PageInfo<>(commentVos);
    }

    public int addCommentFront(CommentVo commentVo) {
        commentVo.setCreateTime(new Date());
        return commentMapper.insertComment(commentVo);
    }
}
